var fileContextPath = "http://127.0.0.1:8080/files";
function getContextPath(){
	return "";
}

/**
 * ajax封装
 * @param url 发送请求的地址
 * @param data 发送到服务器的数据，数组存储，如：{"username": "张三", "password": 123456}
 * @param succCallback 成功回调函数
 * @param isShowLayer 是否显示成功返回消息
 * @param async  同步异步
 * @param dataType 预期服务器返回的数据类型，常用的如：xml、html、json、text
 * @param type 请求方式("POST" 或 "GET")， 默认已经设置为 "POST"
 * @param headers  头部
 * @param timeout  头部
 * @reference jquery-3.3.1.js
 */
function $ajax(options){
    var isShowLoading = options.isShowLoading == undefined ?  true : options.isShowLoading;
    var isShowLayer = options.isShowLayer == undefined ?  true : options.isShowLayer;
    var succCallback = options.succCallback;
    var loading;
	$.ajax({
        type: options.type || "post",
        url: getContextPath() + options.url,
        data: options.data,
        dataType: options.dataType || "json",
        async : options.async == undefined ?  true : options.async,
        timeout: options.timeout || 100000,
        headers : options.headers,
        beforeSend: function(){
        	if(isShowLoading)
        		loading = layer.load(2);
        },
        complete: function(){
        	if(isShowLoading)
        		layer.close(loading);
        },
        success: function(res){
        	if(isShowLayer)
        		layer.msg(res.msg);
            if(res.code == 100){
                window.location.href = getLoginPage();
            }
    		if(succCallback){
                succCallback(res);
            }
        },
        error : function(jqXHR, textStatus, errorThrown){
        	layer.msg(textStatus + "：" + errorThrown);
        },
        statusCode: {
            404: function() {
            	layer.msg("404，请求未找到");
            },
            500:function(){
            	layer.msg("500，服务器内部错误");
            }
        }
    });
}

/**
 * 时间对象的格式化;
 */
Date.prototype.format = function(format) {
    /*
     * eg:format="yyyy-MM-dd hh:mm:ss";
     */
    var o = {
        "M+" : this.getMonth() + 1, // month
        "d+" : this.getDate(), // day
        "h+" : this.getHours(), // hour
        "s+" : this.getSeconds(), // second
        "m+" : this.getMinutes(), // minute
        "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
        "S" : this.getMilliseconds()
        // millisecond
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
            - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

/** 验证是否是空 true 空 false 不是空 */
var isNull = function(text) {
	if (text == null || text == undefined || $.trim(text).length <= 0) {
		text = text + "";
		if (isNaN(text)) {
			return true;
		} else {
			if ($.trim(text).length <= 0) {
				return true;
			}
		}
	}
	return false;
};

var getImgeUrl = function(imgurl, size) {
    if(isNull(imgurl)){
        return "";
    }
    var t = imgurl.split("/");
    var str = fileContextPath;
    var img = "";
    if (size == 0) {
        img = "t_" + t[t.length - 1];
    } else if (size == 1) {
        img = "m_" + t[t.length - 1];
    } else if (size == 2) {
        img = "b_" + t[t.length - 1];
    } else if (size == 3) {
        img = t[t.length - 1];
    }
    for (var i = 0; i < t.length - 1; i++) {
        str = str + t[i] + "/";
    }
    str += img;
    return str;
}

function getCookie(name){
    var offset,cookieValue;
    var search=name+"=";
    if(document.cookie.length>0)
    {
        offset=document.cookie.indexOf(search);
        if(offset!=-1)
        {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1)
                end = document.cookie.length;
            cookieValue=unescape(document.cookie.substring(offset, end));
        }
    }
    return cookieValue;
}
